package com.youdianstar.app.common.network

import okhttp3.Cookie
import okhttp3.HttpUrl
import java.io.Serializable

/**
 * Cookie的序列化包装类
 * 用于将OkHttp的Cookie对象序列化到本地存储
 */
data class SerializableCookie(
    val name: String,
    val value: String,
    val expiresAt: Long,
    val domain: String,
    val path: String,
    val secure: Boolean,
    val httpOnly: Boolean,
    val hostOnly: Boolean,
    val persistent: Boolean
) : Serializable {
    
    companion object {
        /**
         * 将OkHttp Cookie转换为可序列化的Cookie
         */
        fun fromCookie(cookie: Cookie): SerializableCookie {
            return SerializableCookie(
                name = cookie.name,
                value = cookie.value,
                expiresAt = cookie.expiresAt,
                domain = cookie.domain,
                path = cookie.path,
                secure = cookie.secure,
                httpOnly = cookie.httpOnly,
                hostOnly = cookie.hostOnly,
                persistent = cookie.persistent
            )
        }
        
        /**
         * 将可序列化的Cookie转换为OkHttp Cookie
         */
        fun toCookie(serializableCookie: SerializableCookie): Cookie {
            return Cookie.Builder()
                .name(serializableCookie.name)
                .value(serializableCookie.value)
                .expiresAt(serializableCookie.expiresAt)
                .domain(serializableCookie.domain)
                .path(serializableCookie.path)
                .apply {
                    if (serializableCookie.secure) secure()
                    if (serializableCookie.httpOnly) httpOnly()
                    if (serializableCookie.hostOnly) hostOnlyDomain(serializableCookie.domain)
                }
                .build()
        }
    }
}
